<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- 全选框 -->
        <label for="allChecked">全选</label>
        <input type="checkbox" id="allChecked" @change="allSelectFn" :checked="isAllSelect">
        <!-- 购物车列表 -->
        <ul>
            <li v-for="(item,index) in dataArr">
                <input type="checkbox" :checked="item.checked" @change="changeSelectFn(item)">
                商品名称: <span>{{item.name}}</span> |
                商品单价: <span>{{item.price}}</span> |
                数量: <input type="number" min="1" v-model="item.num"> |
                小计: <span>{{item.num*item.price}}</span>
            </li>
        </ul>

        <span>总价:¥ {{total}}</span>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        let vm = new Vue({
            el: "#app",
            data: {
                isAllSelect: false,
                dataArr: [{
                        id: 1,
                        name: "iPhone14",
                        price: "5999",
                        num: 5,
                        checked: false
                    },
                    {
                        id: 2,
                        name: "iPhone14 Pro",
                        price: "7999",
                        num: 1,
                        checked: false
                    },
                    {
                        id: 3,
                        name: "iPhone 14 Pro Max",
                        price: "8999",
                        num: 1,
                        checked: false
                    }
                ]
            },
            computed: {
                total(){
                    //初始值
                    let sum=0;
                    //计算每一项物品总和
                    this.dataArr.forEach(item=>{
                        if(item.checked){
                            sum+=item.num*item.price;
                        }
                    })
                    return sum;
                }
            },
            methods: {
               allSelectFn(){
                this.isAllSelect=!this.isAllSelect;
                this.dataArr.forEach(item=>item.checked=this.isAllSelect)
               },

               changeSelectFn($item){
                $item.checked=!$item.checked;
                let res=this.dataArr.some(item=>!item.checked);
                if(res){
                    this.isAllSelect=false;
                }else{
                    this.isAllSelect=true;
                }
               }
            }
        })
    </script>
</body>

</html>